Hệ thống thông tin tài khoản trong C++
1 #include<iostream>
2 #include<fstream>
3 #include<cstdlib>
4 using std::cout;
5 using std::cin;
6 using std::endl;
7 using std::fstream;
8 using std::ofstream;
9 using std::ifstream;
10 using std::ios;
11 class account_query
12 {
13 private:
14 char account_number[20];
15 char firstName[10];
16 char lastName[10];
17 float total_Balance;
18 public:
19 void read_data();
20 void show_data();
21 void write_rec();
22 void read_rec();
23 void search_rec();
24 void edit_rec();
25 void delete_rec();
26 };
27 void account_query::read_data()
28 {
29 cout<<"\nEnter Account Number: ";
30 cin>>account_number;
31 cout<<"Enter First Name: ";
32 cin>>firstName;
33 cout<<"Enter Last Name: ";
34 cin>>lastName;
35 cout<<"Enter Balance: ";
36 cin>>total_Balance;
37 cout<<endl;
38 }
39 void account_query::show_data()
40 {
41 cout<<"Account Number: "<<account_number<<endl;
42 cout<<"First Name: "<<firstName<<endl;
43 cout<<"Last Name: "<<lastName<<endl;
44 cout<<"Current Balance: Rs. "<<total_Balance<<endl;
45 cout<<"-------------------------------"<<endl;
46 }
47 void account_query::write_rec()
48 {
49 ofstream outfile;
50 outfile.open("record.bank", ios::binary|ios::app);
51 read_data();
52 outfile.write(reinterpret_cast<char *>(this), sizeof(*this));
53 outfile.close();
54 }
55 void account_query::read_rec()
56 {
57 ifstream infile;
58 infile.open("record.bank", ios::binary);
59 if(!infile)
60 {
61 cout<<"Error in Opening! File Not Found!!"<<endl;
62 return;
63 }
64 cout<<"\n****Data from file****"<<endl;
65 while(!infile.eof())
66 {
67 if(infile.read(reinterpret_cast<char*>(this), sizeof(*this))>0)
68 {
69 show_data();
70 }
71 }
72 infile.close();
73 }
74 void account_query::search_rec()
75 {
76 int n;
77 ifstream infile;
78 infile.open("record.bank", ios::binary);
79 if(!infile)
80 {
81 cout<<"\nError in opening! File Not Found!!"<<endl;
82 return;
83 }
84 infile.seekg(0,ios::end);
85 int count = infile.tellg()/sizeof(*this);
86 cout<<"\n There are "<<count<<" record in the file";
87 cout<<"\n Enter Record Number to Search: ";
88 cin>>n;
89 infile.seekg((n-1)*sizeof(*this));
90 infile.read(reinterpret_cast<char*>(this), sizeof(*this));
91 show_data();
92 }
93 void account_query::edit_rec()
94 {
95 int n;
96 fstream iofile;
97 iofile.open("record.bank", ios::in|ios::binary);
98 if(!iofile)
99 {
100 cout<<"\nError in opening! File Not Found!!"<<endl;
101 return;
102 }
103 iofile.seekg(0, ios::end);
104 int count = iofile.tellg()/sizeof(*this);
105 cout<<"\n There are "<<count<<" record in the file";
106 cout<<"\n Enter Record Number to edit: ";
107 cin>>n;
108 iofile.seekg((n-1)*sizeof(*this));
109 iofile.read(reinterpret_cast<char*>(this), sizeof(*this));
110 cout<<"Record "<<n<<" has following data"<<endl;
111 show_data();
112 iofile.close();
113 iofile.open("record.bank", ios::out|ios::in|ios::binary);
114 iofile.seekp((n-1)*sizeof(*this));
115 cout<<"\nEnter data to Modify "<<endl;
116 read_data();
117 iofile.write(reinterpret_cast<char*>(this), sizeof(*this));
118 }
119 void account_query::delete_rec()
120 {
121 int n;
122 ifstream infile;
123 infile.open("record.bank", ios::binary);
124 if(!infile)
125 {
126 cout<<"\nError in opening! File Not Found!!"<<endl;
127 return;
128 }
129 infile.seekg(0,ios::end);
130 int count = infile.tellg()/sizeof(*this);
131 cout<<"\n There are "<<count<<" record in the file";
132 cout<<"\n Enter Record Number to Delete: ";
133 cin>>n;
134 fstream tmpfile;
135 tmpfile.open("tmpfile.bank", ios::out|ios::binary);
136 infile.seekg(0);
137 for(int i=0; i<count; i++)
138 {
139 infile.read(reinterpret_cast<char*>(this),sizeof(*this));
140 if(i==(n-1))
141 continue;
142 tmpfile.write(reinterpret_cast<char*>(this), sizeof(*this));
143 }
144 infile.close();
145 tmpfile.close();
146 remove("record.bank");
147 rename("tmpfile.bank", "record.bank");
148 }
149 int main()
150 {
151 account_query A;
152 int choice;
153 cout<<"***Acount Information System***"<<endl;
154 while(true)
155 {
156 cout<<"Select one option below ";
157 cout<<"\n\t1-->Add record to file";
158 cout<<"\n\t2-->Show record from file";
159 cout<<"\n\t3-->Search Record from file";
160 cout<<"\n\t4-->Update Record";
161 cout<<"\n\t5-->Delete Record";
162 cout<<"\n\t6-->Quit";
163 cout<<"\nEnter your choice: ";
164 cin>>choice;
165 switch(choice)
166 {
167 case 1:
168 A.write_rec();
169 break;
170 case 2:
171 A.read_rec();
172 break;
173 case 3:
174 A.search_rec();
175 break;
176 case 4:
177 A.edit_rec();
178 break;
179 case 5:
180 A.delete_rec();
181 break;
182 case 6:
183 exit(0);
184 break;
185 default:
186 cout<<"\nEnter corret choice";
187 exit(0);
188 }
189 }
190 system("pause");
191 return 0;
192 }